/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[Polymorphism.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package objectOriented;

/**
 * 多态
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class Polymorphism {

	public static void main(String[] args) {
		SuperClass subClass = new SubClass(3);
		// 下面的代码输出了103, 原因在于在调用Super类的构造方法时, 由于多态, this其实是子类对象,
		// 因此调用的是子类的setId的方法
		System.out.println(subClass.getId());

		SuperClass superClass = new SuperClass(3);
		System.out.println(superClass.getId());
	}
}

/**
 * 父类
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class SuperClass {

	private int id;

	public SuperClass(int id) {
		this.setId(id);
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}
}

/**
 * 子类
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class SubClass extends SuperClass {

	public SubClass(int id) {
		super(id);
		this.setId(id);
	}

	@Override
	public void setId(int id) {
		super.setId(id + 100);
	}
}
